package mr.ali.webvideo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mr.ali.webvideo.model.Collection;
import mr.ali.webvideo.model.CollectionRecommend;
import mr.ali.webvideo.mapper.CollectionRecommendMapper;
import mr.ali.webvideo.service.ICollectionRecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mr.ali.webvideo.service.ICollectionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-08-16
 */
@Service
public class CollectionRecommendServiceImpl extends ServiceImpl<CollectionRecommendMapper, CollectionRecommend> implements ICollectionRecommendService {

    @Resource
    private CollectionRecommendMapper collectionRecommendMapper;

    @Resource
    private ICollectionService collectionService;

    @Override
    public PageInfo<CollectionRecommend> findAll(Integer pageNum, Integer pageSize, Integer userId, Integer videoId) {
        QueryWrapper<CollectionRecommend> queryWrapper = new QueryWrapper<>();
        if (userId != null && userId != 0) {
            queryWrapper.eq("user_id", userId);
        }
        if (videoId != null) {
            queryWrapper.eq("video_id", videoId);
        }
        queryWrapper.orderByDesc("gmt_create");
        PageHelper.startPage(pageNum, pageSize);
        List<CollectionRecommend> tags = collectionRecommendMapper.selectList(queryWrapper);
        for (CollectionRecommend video : tags) {
            List<Collection> videos = collectionService.videoTagsById(video.getVideoId());
            video.setCollection(videos);
        }
        return new PageInfo<>(tags);
    }

    @Override
    public Integer delete(Integer userId, Integer videoId) {
        QueryWrapper<CollectionRecommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("video_id", videoId);
        return collectionRecommendMapper.delete(queryWrapper);
    }

    @Override
    public Integer insert(Integer userId, Integer videoId) {
        QueryWrapper<CollectionRecommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("video_id", videoId);
        Long row = collectionRecommendMapper.selectCount(queryWrapper);
        if (row < 1) {
            CollectionRecommend collectionRecommend = new CollectionRecommend();
            collectionRecommend.setUserId(userId);
            collectionRecommend.setVideoId(videoId);
            collectionRecommend.setGmtCreate(System.currentTimeMillis());
            collectionRecommendMapper.insert(collectionRecommend);
            return 1;
        } else if (row > 0) {
            collectionRecommendMapper.delete(queryWrapper);
            return 2;
        }
        return 0;
    }
}
